/*
 * @Author: jackning 270580156@qq.com
 * @Date: 2024-06-21 15:00:31
 * @LastEditors: jackning 270580156@qq.com
 * @LastEditTime: 2024-09-24 09:15:44
 * @Description: bytedesk.com https://github.com/Bytedesk/bytedesk
 *   Please be aware of the BSL license restrictions before installing Bytedesk IM –
 *  selling, reselling, or hosting Bytedesk IM as a service is a breach of the terms and automatically terminates your rights under the license.
 *  仅支持企业内部员工自用，严禁私自用于销售、二次销售或者部署SaaS方式销售
 *  Business Source License 1.1: https://github.com/Bytedesk/bytedesk/blob/main/LICENSE
 *  contact: 270580156@qq.com
 *  技术/商务联系：270580156@qq.com
 * Copyright (c) 2024 by bytedesk.com, All Rights Reserved.
 */

import { message } from "@/AntdGlobalComp";
import { changePassword } from "@/apis/core/user";
import useTranslate from "@/hooks/useTranslate";
import { ProForm, ProFormText } from "@ant-design/pro-components";
import { Modal } from "antd";
// import { useState } from "react";
import { useIntl } from "react-intl";

// 
type ChangePasswordModelProps = {
    open: boolean,
    // onSubmit: () => void,
    onClose: () => void,
}
const ChangePasswordModel = ({ open, onClose }: ChangePasswordModelProps) => {
    const intl = useIntl();
    // const [form] = ProForm.useForm();
    const { translateString } = useTranslate();
    
    const handleOk = () => {
        onClose();
    }
    const handleCancel = () => {
        onClose();
    }

    return (
        <>
            <Modal title={intl.formatMessage({
                id: "pages.settings.reset.password",
                defaultMessage: "重置密码",
            })}
                forceRender
                open={open}
                footer={null}
                onCancel={handleCancel}>
                <ProForm
                    // form={form}
                    initialValues={{
                        oldPassword: '',
                        newPassword: '',
                        confirmPassword: '',
                    }}
                    onFinish={async (values) => {
                        console.log('changePassword:', values);
                        if (values.newPassword.trim().length < 6) {
                            message.error('密码最小长度不能小于6');
                            return;
                        }
                        if (values.newPassword !== values.confirmPassword) {
                            message.error('两次输入密码不一致');
                            return;
                        }
                        const user: USER.UserResponse = {
                            oldPassword: values.oldPassword,
                            newPassword: values.newPassword,
                        }
                        const response = await changePassword(user);
                        console.log('changePassword response:', response);
                        if (response.data.code === 200) {
                            message.success('Password changed successfully!');
                            handleOk();
                        } else {
                            message.error(translateString(response.data.message));
                        }
                    }}
                >
                    <ProFormText.Password name="oldPassword" label="原密码(手机号直接登录用户，可以留空)" />
                    <ProFormText.Password name="newPassword" label="新密码" />
                    <ProFormText.Password name="confirmPassword" label="确认密码" />
                </ProForm>
            </Modal>
        </>
    )
}
export default ChangePasswordModel;